package cn.ococci.dm.Main.View;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import java.lang.reflect.Field;

import cn.ococci_dm.R;


public class ClearEditText extends EditText implements
        OnFocusChangeListener, TextWatcher {

    private Drawable mClearDrawable; // 清除内容按钮
    private boolean hasFocus; // 是否聚焦

    public ClearEditText(Context context) {
        super(context, null);
        initCustomEditText();
    }

    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCustomEditText();
    }

    public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initCustomEditText();
    }


    private void initCustomEditText() {
        this.setCursorVisible(true);
        try {
            Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
            field.setAccessible(true);  //设置访问域到private
            field.set(this, R.drawable.edit_dis_selected);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        mClearDrawable = getCompoundDrawables()[2];
        if (mClearDrawable == null) {
            mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);
        }
        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(),
                mClearDrawable.getIntrinsicHeight());
        setClearIconVisible(false);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);


    }

    /**
     * 根据是否显示来重新绘制清楚图标
     *
     * @param isShowClearIcon :清除图标是否显示
     */
    private void setClearIconVisible(boolean isShowClearIcon) {
        Drawable locationRight = isShowClearIcon ? mClearDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0],
                getCompoundDrawables()[1], locationRight,
                getCompoundDrawables()[3]);

    }



    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        this.hasFocus = hasFocus;
        if (hasFocus) {
            setClearIconVisible(getText().length() > 0);
        } else {
            setClearIconVisible(false);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) { // 内容改变之前调用(实际上内容并没有发生改变)
    }

    @Override
    public void afterTextChanged(Editable s) { // 在EditText中的内容被改变之后就被调用
    }

    @Override
    public void onTextChanged(CharSequence text, int start, int lengthBefore,
                              int lengthAfter) { // text是原有的文本
        // 从start开始的lengthAfter个字符替换旧的长度为lengthBefore的旧文本

        if (hasFocus) {
            setClearIconVisible(text.length() > 0);

        } else {

            setClearIconVisible(false);
        }
    }

    /**
     * getTotalPaddingRight() : 清除小图标左边缘到 控件有边缘的距离 getPaddingRight() :
     * 清楚小图标右边缘到控件右边缘之间的距离
     */
    @SuppressWarnings("unused")
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (mClearDrawable != null
                && event.getAction() == MotionEvent.ACTION_UP) {

            int x = (int) event.getX();
            boolean horizontalWidth = x > (this.getWidth() - getTotalPaddingRight())
                    && x < (this.getWidth() - getPaddingRight());

            Rect rect = new Rect();     // 获取删除图标的边界位置

            int ClearIconHeight = rect.height();
            int y = (int) event.getY();
            int distance = (getHeight() - ClearIconHeight) / 2; // 获取底部之间的距离

            boolean VerticalHeight = y > distance
                    && y < (distance + ClearIconHeight);

            if (horizontalWidth) {
                this.setText("");
            }
        }
        return super.onTouchEvent(event);
    }

}
